package com.ruoyi.statistics.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.labdata.domain.LabOrgnization;
import com.ruoyi.labdata.service.ILabOrgnizationService;
import com.ruoyi.statistics.domain.LabStatisticsBaseCollege;
import com.ruoyi.statistics.domain.LabStatisticsEquipment;
import com.ruoyi.statistics.domain.LabStatisticsPerson;
import com.ruoyi.statistics.domain.LabStatisticsProject;
import com.ruoyi.statistics.mapper.LabStatisticsProjectMapper;
import com.ruoyi.statistics.service.ILabStatisticsProjectService;
import java.util.List;

import com.ruoyi.system.domain.LabProjectDevelopmentProject;
import com.ruoyi.system.domain.ProjectCreation;
import com.ruoyi.system.service.ILabProjectDevelopmentProjectService;
import com.ruoyi.system.service.IProjectCreationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 教学试验项目Service业务层处理
 * 
 * @author jun.wang
 * @date 2022-05-21
 */
@Service
public class LabStatisticsProjectServiceImpl implements ILabStatisticsProjectService 
{
    @Autowired
    private LabStatisticsProjectMapper labStatisticsProjectMapper;

    @Autowired
    private IProjectCreationService projectCreationService;

    @Autowired
    private ILabProjectDevelopmentProjectService projectDevelopmentProjectService;

    @Autowired
    private ILabOrgnizationService orgnizationService;

    /**
     * 查询教学试验项目
     * 
     * @param id 教学试验项目ID
     * @return 教学试验项目
     */
    @Override
    public LabStatisticsProject selectLabStatisticsProjectById(Long id)
    {
        return labStatisticsProjectMapper.selectLabStatisticsProjectById(id);
    }

    /**
     * 查询教学试验项目列表
     * 
     * @param labStatisticsProject 教学试验项目
     * @return 教学试验项目
     */
    @Override
    public List<LabStatisticsProject> selectLabStatisticsProjectList(LabStatisticsProject labStatisticsProject)
    {
        return labStatisticsProjectMapper.selectLabStatisticsProjectList(labStatisticsProject);
    }

    /**
     * 新增教学试验项目
     * 
     * @param labStatisticsProject 教学试验项目
     * @return 结果
     */
    @Override
    public int insertLabStatisticsProject(LabStatisticsProject labStatisticsProject)
    {
        return labStatisticsProjectMapper.insertLabStatisticsProject(labStatisticsProject);
    }

    /**
     * 修改教学试验项目
     * 
     * @param labStatisticsProject 教学试验项目
     * @return 结果
     */
    @Override
    public int updateLabStatisticsProject(LabStatisticsProject labStatisticsProject)
    {
        labStatisticsProject.setUpdateTime(DateUtils.getNowDate());
        return labStatisticsProjectMapper.updateLabStatisticsProject(labStatisticsProject);
    }

    /**
     * 批量删除教学试验项目
     * 
     * @param ids 需要删除的教学试验项目ID
     * @return 结果
     */
    @Override
    public int deleteLabStatisticsProjectByIds(Long[] ids)
    {
        return labStatisticsProjectMapper.deleteLabStatisticsProjectByIds(ids);
    }

    /**
     * 删除教学试验项目信息
     * 
     * @param id 教学试验项目ID
     * @return 结果
     */
    @Override
    public int deleteLabStatisticsProjectById(Long id)
    {
        return labStatisticsProjectMapper.deleteLabStatisticsProjectById(id);
    }

  @Override
  public List<LabStatisticsProject> selectLabStatisticsProjectByBaseCollegeId(Long baseCollegeId) {
    return labStatisticsProjectMapper.selectLabStatisticsProjectByBaseCollegeId(baseCollegeId);
  }

    @Override
    public void addProject(LabStatisticsBaseCollege baseCollege, List<LabOrgnization> labOrgnizations) {
        Long baseCollegeId = baseCollege.getId();
        deleteLabStatisticsProjectByBaseCollegeId(baseCollegeId);

        for (LabOrgnization labOrgnization : labOrgnizations) {

            LabProjectDevelopmentProject query = new LabProjectDevelopmentProject();
            query.setLaboratoryId(labOrgnization.getOrgId());
            List<LabProjectDevelopmentProject> projectDevelopmentProjects = projectDevelopmentProjectService.selectLabProjectDevelopmentProjectList(query);


            for (LabProjectDevelopmentProject developmentProject : projectDevelopmentProjects) {

                LabStatisticsProject project = new LabStatisticsProject();
                project.setBaseCollegeId(baseCollegeId);
                project.setLabId(labOrgnization.getOrgId());
                project.setLabCode(labOrgnization.getOrgCode());
                project.setLabName(labOrgnization.getOrgName());

                LabOrgnization colleageOrg = orgnizationService.selectLabOrgnizationById(developmentProject.getDepartmentOrgId());
                project.setCollegeCode(colleageOrg.getOrgCode()); // 学院代码

                ProjectCreation projectCreation = projectCreationService.selectProjectCreationById(developmentProject.getInstanceId());

                project.setName(projectCreation.getProjectName());
                project.setCategory(null); // 项目类别
                project.setType(developmentProject.getProjectType()); // 实验类型
                project.setPersonNum(developmentProject.getReportInstructedStudentsNumber()); // 人数
                project.setPersonsOfGroup(developmentProject.getReportGroupPerNumber()); // 每组人数
                if (StringUtils.isNotEmpty(developmentProject.getReportingHours())) { // 实验学时数
                    project.setExperimentalHours(Long.valueOf(developmentProject.getReportingHours()));
                }

                insertLabStatisticsProject(project);
            }

        }
    }

  @Override
  public int deleteLabStatisticsProjectByBaseCollegeId(Long baseCollegeId) {
        return labStatisticsProjectMapper.deleteLabStatisticsProjectByBaseCollegeId(baseCollegeId);
  }

  @Override
  @Transactional
  public void importData(List<LabStatisticsProject> projectDtos, Long baseCollegeId) {
    this.deleteLabStatisticsProjectByBaseCollegeId(baseCollegeId);
      for(LabStatisticsProject project : projectDtos){
          project.setBaseCollegeId(baseCollegeId);
          this.insertLabStatisticsProject(project);
      }
  }
}
